<?php
namespace App\HttpController\Admin;
use App\Lib\Tool\Pwd;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\DbManager;
use EasySwoole\Session\Session;
use EasySwoole\Utility\Random;
use EasySwoole\VerifyCode\Conf;

class Tool extends Sys
{

    function index(){}


    //生成验证码
    function createVerifyCode()
    {
        $config = new Conf();
        $code = new \EasySwoole\VerifyCode\VerifyCode($config);
        $random = Random::character(4, '1234567890abcdefghijklmnopqrstuvwxyz');
        $obj = $code->DrawCode($random);
        $pic = $obj->getImageByte();
        Session::getInstance()->set('code',$random);
        $this->response()->withHeader('Content-Type','image/png');
        $this->response()->write($pic);
    }

    public function upload()
    {
        $request=  $this->request();
        $img_file = $request->getUploadedFile('file');//获取一个上传文件,返回的是一个\EasySwoole\Http\Message\UploadFile的对象

        $route = EASYSWOOLE_ROOT.'/'.SYS_DIR_IMG.'image/';
        $fileName = md5(rand(10000,99999).time());
        $ext = $img_file->getClientMediaType();
        $ext = explode('/',$ext);
        $filePath = $route.$fileName.'.'.$ext[1];
        try{
            $result = $img_file->moveTo($filePath);
            if($result){
                $this->ajaxSuccess(['route'=>SYS_DIR_IMG.'image/'.$fileName.'.'.$ext[1]]);
            }
        }catch (\Exception $exception){
            $this->ajaxError(0,$exception->getMessage());
        }


    }


    public function mysql(){

        $queryBuild = new QueryBuilder();

        $datas = [];
        if($this->request()->getRequestParam('sqls')){
            $jsMsg = '';
            if($this->request()->getRequestParam('sqls')){
                $uid= 0;//is_login();
                $uidArr = array(0,1);
                $timeMicro=explode(' ', microtime());
                $startTime=$timeMicro[1].$timeMicro[0];
                $startTime_u=time();
                $sql = $this->request()->getRequestParam('sqls');
                $sqls = $sql;
                if(in_array($uid,$uidArr)){
                    if(!empty($sql)){
                        $sqlArr=explode(';',$sql);

                        foreach ($sqlArr as $key => $value) {
                            $value = trim($value);
                            if(!empty($value)){
                                $tempValue = strtolower($value);
                                $queryFunction = '';
                                if(strstr($tempValue,'update ') || strstr($tempValue,'delete ') || strstr($tempValue,'insert ')){
                                    $queryFunction = 'execute';
                                    $queryBuild->raw($value);
                                    $r = DbManager::getInstance()->query($queryBuild, true, 'wr');
                                    $_count = $r->getTotalCount();
                                }else{
                                    $queryFunction = 'query';
                                    $queryBuild->raw($value);
                                    $r = DbManager::getInstance()->query($queryBuild, true, 'wr');
                                    $_count =  $r->getTotalCount();
                                }
                                $result['sql--'.($key+1).' 总共 '.$_count.' 行  执行方式：'.$queryFunction] = $r->getResult();
                            }
                        }
                    }
                }
                else{
                    $jsMsg='你的梦想实现不了！';
                }
                $sqlsHight=$this->request()->getRequestParam('sqlsHight');
                $sqlsHight=empty($sqlsHight)?100:$sqlsHight;
                $timeMicro=explode(' ', microtime());
                $endTime=$timeMicro[1].$timeMicro[0];
                $endTime_u=time();
                $datas = [
                    'sqlsHight'     => $sqlsHight,
                    'startTime'     => $startTime,
                    'startTime_u'   => $startTime_u,
                    'endTime'       => $endTime,
                    'endTime_u'     => $endTime_u,
                    'result'        => $result,
                    'jsMsg'         => $jsMsg,
                    'sqls'          => $sqls,
                ];
            }
        }

        $this->show('admin/tool/mysql',$datas);
    }

}